//
//  LZRestaurantPresenter.m
//  laziz_Consumer
//
//  Created by xbnzd on 16/11/21.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZRestaurantPresenter.h"
#import "NetworkServiceManager.h"
#import "LZUserModel.h"
#import "LZRestaurantListModel.h"
#import "LZRestaurantDetailModel.h"
#import "LAReviewModel.h"
#import "LZReviewCountModel.h"
#import "LZRestaurantPhotoAlbumModel.h"
#import "LZVideoListModel.h"
#import "MJExtension.h"
#import "LZMyCouponsModel.h"

extern NSString *customer;

static NSString *kRestaurantListUrl = @"/clientsellerUser/listPage";  //餐厅列表
static NSString *kRestaurantDetailUrl = @"/clientsellerUser/info";    //餐厅详情
static NSString *kRestaurantCommentCountUrl = @"/clientrestaurantComments/commentCount";  //餐厅评论数量统计
static NSString *kRestaurantCommentListUrl = @"/clientrestaurantComments/listPageBySellerId"; //餐厅评论列表
static NSString *kRestaurantVideoListUrl = @"/clientsellerUser/listPagePromotion";       //餐厅详情视频列表
static NSString *kRestaurantPhotoAlbumUrl = @"/clientsellerPhotoAlbum/listBySellerId";   //餐厅评论数量统计
static NSString *kRestaurantPromotionListUrl = @"/clientsalesPromotion/listByUserAndSeller"; // 优惠买单列表
static NSString *kRestaurantSearchAddUrl = @"/clientsellerUser/searchSave";              //餐厅搜索次数+1

@implementation LZRestaurantPresenter


/**
 餐厅列表

 @param dic 筛选的参数
 @param pageNum 页数
 @param pageLimit 每页的数量
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getRestaurantListWithFilterPrameter:(NSDictionary *)dic PageNum:(int)pageNum pageLimit:(int)pageLimit ResultSuccess:(void (^)(NSMutableArray *,int,NSTimeInterval))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kRestaurantListUrl];
    NSMutableDictionary *prameter = [[NSMutableDictionary alloc] init];
    if (dic.count != 0) {
        prameter = [NSMutableDictionary dictionaryWithDictionary:dic];
    }
    
    prameter[@"cpage"] = @(pageNum);
    prameter[@"limit"] = @(pageLimit);
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:prameter success:^(NSURLSessionDataTask *task, id resultData) {
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            NSMutableArray *arrData = [NSMutableArray array];
            NSArray *arrResult = resultData[@"data"][@"list"];
            for (int i = 0; i<arrResult.count; i++) {
                LZRestaurantListModel *model = [LZRestaurantListModel mj_objectWithKeyValues:arrResult[i]];
                [model changeCuisineNameByCuisineID];
                [arrData addObject:model];
            }
            
            NSTimeInterval currentTimeTemp  = [resultData[@"timestamp"] doubleValue];

            if (successBlock) {
                successBlock(arrData,[resultData[@"data"][@"cPageNO"] intValue],currentTimeTemp);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}



/**
 餐厅详情

 @param restaurantId 商户id
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getRestaurantDetailWithRestaurantID:(NSString *)restaurantId ResultSuccess:(void (^)(id result))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{

    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kRestaurantDetailUrl];
    NSMutableDictionary *dic = [@{
                          @"id" : restaurantId,
                          } mutableCopy];
    
    if ([LZUserModel Instance].userToken.length > 0) {
        dic[@"token"] = [LZUserModel Instance].userToken;
    }
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        DLog(@"%@",resultData);
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {

            LZRestaurantDetailModel *model = [LZRestaurantDetailModel mj_objectWithKeyValues:resultData[@"data"]];
            
            [model changeCuisineNameByCuisineID];
            
            if (successBlock) {
                successBlock(model);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}




/**
 餐厅评论统计

 @param restaurantId <#restaurantId description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getRestaurantCommentTotleNumWithRestaurantID:(NSString *)restaurantId ResultSuccess:(void(^)(id result))successBlock ResultFailed:(void(^)(NSString *remind))failedBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kRestaurantCommentCountUrl];
    urlStr = [urlStr stringByAppendingString:[NSString stringWithFormat:@"?id=%@",restaurantId]];
    
    [[NetworkServiceManager manager] executGetTask:urlStr parameters:nil success:^(NSURLSessionDataTask *task, id resultData) {
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            
            LZReviewCountModel *model = [LZReviewCountModel mj_objectWithKeyValues:resultData[@"data"]];
            if (successBlock) {
                successBlock(model);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];

}



/**
 餐厅详情评论列表

 @param restaurantId 餐厅id
 @param pageNum 页数
 @param pageLimit 数量
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getRestaurantDetailCommentListWithPrameter:(NSDictionary*)prameter RestaurantID:(NSString *)restaurantId PageNum:(int)pageNum PageLimit:(int)pageLimit ResultSuccess:(void (^)(NSArray *,NSNumber *))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kRestaurantCommentListUrl];
    
    NSMutableDictionary *dic = prameter?[prameter mutableCopy]:[@{} mutableCopy];
    dic[@"sellerId"] = restaurantId;
    dic[@"cpage"] = [NSNumber numberWithInt:pageNum];
    dic[@"limit"] = [NSNumber numberWithInt:pageLimit];

    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            NSMutableArray *arrData = [NSMutableArray array];
            NSArray *arrResult = resultData[@"data"][@"list"];
            for (int i = 0; i<arrResult.count; i++) {
                LAReviewModel *model = [LAReviewModel mj_objectWithKeyValues:arrResult[i]];
                [arrData addObject:model];
            }
            if (successBlock) {
                successBlock(arrData,resultData[@"timestamp"]);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }

    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}

/**
 餐厅详情中的视频列表

 @param restaurantId 餐厅id
 @param pageNum 页数
 @param pageLimit 数量
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getRestaurantDetailVideoListWithPrameter:(NSDictionary*)prameter RestaurantID:(NSString *)restaurantId PageNum:(int)pageNum PageLimit:(int)pageLimit ResultSuccess:(void (^)(NSArray *,NSNumber *))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kRestaurantVideoListUrl];
    
    NSMutableDictionary *dic = prameter?[prameter mutableCopy]:[@{} mutableCopy];
    dic[@"id"] = restaurantId;
    dic[@"cpage"] = [NSNumber numberWithInt:pageNum];
    dic[@"limit"] = [NSNumber numberWithInt:pageLimit];
    
    if ([LZUserModel Instance].userToken.length > 0) {
        dic[@"token"] = [LZUserModel Instance].userToken;
    }
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            NSMutableArray *listArrTemp = [NSMutableArray array];
            NSArray *arrTemp = resultData[@"data"][@"list"];
            for (int i = 0; i < arrTemp.count; i ++) {
                NSDictionary *dicTemp = arrTemp[i];
                LZVideoListModel *m = [LZVideoListModel mj_objectWithKeyValues:dicTemp];
                [listArrTemp addObject:m];
            }

            if (successBlock) {
                successBlock(listArrTemp,resultData[@"timestamp"]);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}

/**
 获取餐厅相册
 
 @param restaurantId <#restaurantId description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getRestaurantPhotoAlbumWithRestaurantID:(NSString *)restaurantId ResultSuccess:(void(^)(NSArray *result))successBlock ResultFailed:(void(^)(NSString *remind))failedBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kRestaurantPhotoAlbumUrl];
    urlStr = [urlStr stringByAppendingString:[NSString stringWithFormat:@"?sellerId=%@",restaurantId]];
    
    [[NetworkServiceManager manager] executGetTask:urlStr parameters:nil success:^(NSURLSessionDataTask *task, id resultData) {
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            
            NSMutableArray *arrData = [NSMutableArray array];
            
            NSArray *arrResult = [resultData objectForKey:@"data"];
            for (int i = 0; i<arrResult.count; i++) {
                // 相片类型（1菜，2环境，3菜单）
                LZRestaurantPhotoAlbumModel *model = [LZRestaurantPhotoAlbumModel mj_objectWithKeyValues:arrResult[i]];
                [arrData addObject:model];
            }
            
            if (successBlock) {
                successBlock(arrData);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
    
}

/**
 优惠买单列表
 
 @param restaurantId <#restaurantId description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getRestaurantPromotionListWithRestaurantID:(NSString *)restaurantId ResultSuccess:(void(^)(NSArray *result))successBlock ResultFailed:(void(^)(NSString *remind))failedBlock {
    
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kRestaurantPromotionListUrl];
    NSDictionary *dic = @{
                          @"sellerId" : restaurantId,
                          @"token" : [LZUserModel Instance].userToken
                          };
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            NSMutableArray *arrData = [NSMutableArray array];
            
            NSArray *arrResult = [resultData objectForKey:@"data"];
            for (int i = 0; i<arrResult.count; i++) {
                LZMyCouponsModel *model = [LZMyCouponsModel mj_objectWithKeyValues:arrResult[i]];
                [arrData addObject:model];
            }
            
            if (successBlock) {
                successBlock(arrData);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}


/**
 餐厅搜索次数+1

 @param restaurantId 餐厅id
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)restaurantSearchAddWithRestaurantID:(NSString *)restaurantId ResultSuccess:(void (^)(void))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@?id=%@",customer,kRestaurantSearchAddUrl,restaurantId];
    [[NetworkServiceManager manager] executGetTask:urlStr parameters:nil success:^(NSURLSessionDataTask *task, id resultData) {
        NSDictionary *dic = (NSDictionary *)resultData;
        if ([dic[@"statusCode"] integerValue] == kCodeSuccesss) {
            if (successBlock) {
                successBlock();
            }
        }else{
            NSString *str = dic[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}










@end
